package cn.stylefeng.guns.sys.modular.system.controller;

import cn.stylefeng.guns.sys.core.util.MessageType;
import cn.stylefeng.guns.sys.modular.system.entity.BtyWx;
import cn.stylefeng.guns.sys.modular.system.entity.Dict;
import cn.stylefeng.guns.sys.modular.system.service.BtyWxService;
import cn.stylefeng.guns.sys.modular.system.service.DictService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/weixin")
public class WeixinController {
    @Resource
    private DictService dictService;
    @Resource
    private BtyWxService btyWxService;
    private Logger logger = LoggerFactory.getLogger(WeixinController.class);
    @RequestMapping(value = "")
    @ResponseBody
    public Object sendMsg() {
        RestTemplate restTemplate = new RestTemplate();
        Dict dict = dictService.getOne(new QueryWrapper<Dict>().eq("code", "access_token"));
        String accessToken = "";
        if (ToolUtil.isNotEmpty(dict)) {
            accessToken = dict.getName();
        }
        //通过openid给指定用户发送消息
        String url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token="+accessToken;
        String s="";
        //财务部27 人事部1268783244079915009  技术部25
       List<BtyWx> wxList=btyWxService.list(new QueryWrapper<BtyWx>().in("openid",new Object[]{"oM18PwW_aRtOaOH0F-YwsmMxT2sM"}));
//       List<BtyWx> wxList=btyWxService.list(new QueryWrapper<BtyWx>(). notIn("dept_id",new Object[]{"25"}));
       //List<BtyWx> wxList=btyWxService.list(new QueryWrapper<BtyWx>());
        for (int i = 0; i <wxList.size() ; i++) {
           String toUser=wxList.get(i).getOpenid();
            String msgType = MessageType.TEXT_MESSAGE;
            JSONObject contentJson = new JSONObject();
            String name=wxList.get(i).getName()==null?"同学":wxList.get(i).getName();
            String content="不多不少，一条就好；不早不晚，时间刚好；不深不浅，真心就好；不骄不躁，自在逍遥。电波传达，祝福捎上，祝你快乐，看到以后请回复微笑，让我们一起拥抱美好！早安，"+name;
            contentJson.put("content",content);

            JSONObject json = new JSONObject();
            json.put("touser", toUser);
            json.put("msgtype", msgType);
            json.put("text", contentJson);
            HttpHeaders headers = new HttpHeaders();
            MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
            headers.setContentType(type);
            headers.add("Accept", MediaType.APPLICATION_JSON.toString());
            HttpEntity<String> formEntity = new HttpEntity<String>(json.toString(), headers);

            s= restTemplate.postForEntity(url,formEntity,String.class).getBody();
            s=toUser+s;
            logger.info("===openid==="+s);
            return s;
        }
        //String touser ="oM18PwW_aRtOaOH0F-YwsmMxT2sM";
        //String touser ="oM18PwQBM_f-4xlF7DKwnLQZ7XXc";
        //String touser ="oM18PwWQsdIP3QEKcWejL1tF3X8s";
        //消息类型
//        String msgType = MessageType.TEXT_MESSAGE;
//        JSONObject contentJson = new JSONObject();
//        String content="天才,加油!!!";
//        contentJson.put("content",content);
//
//        JSONObject json = new JSONObject();
//        json.put("touser", touser);
//        json.put("msgtype", msgType);
//        json.put("text", contentJson);
//        HttpHeaders headers = new HttpHeaders();
//        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
//        headers.setContentType(type);
//        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
//        HttpEntity<String> formEntity = new HttpEntity<String>(json.toString(), headers);
//        String s= restTemplate.postForEntity(url,formEntity,String.class).getBody();
       return null;

    }
}
